from django import http

import constants
from apps.company.models import LabelCompany, Company, FoundTeam, CompanyProduct, CompanyDomain, InvestCompany
from apps.position.models import Position


def company_others(first_form, tag, founder_team, product, user):
    """添加公司信息"""

    # 查询公司列表
    try:
        company = Company.objects.get(user_id=user.id)
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})

    # 为公司添加标签
    try:
        for label in tag:
            labobj = LabelCompany.objects.create(
                name=label,
                company=company,
            )
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    print('公司标签添加OK')
    # 公司创世团队
    try:
        founder = FoundTeam.objects.create(
            person_name=founder_team.get('name'),
            cur_job=founder_team.get('position'),
            sina_url=founder_team.get('weibo'),
            header_image=founder_team.get('founder_image'),
            intro=founder_team.get('remark'),
            company=company,
        )
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    print('公司团队OK')
    # 公司产品
    try:
        produ = CompanyProduct.objects.create(
            poster_url=product.get("poster_url"),
            name=product.get('name'),
            url=product.get('product_url'),
            intro=product.get('product_profile'),
            company=company,
        )
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    print('公司产品OK')
    # 公司领域
    try:
        lingyu = CompanyDomain.objects.create(
            name=first_form.get("industry"),
            company=company,
        )
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    print('公司领域OK')
    # 投资公司
    try:
        invest = InvestCompany.objects.create(
            name=first_form.get("dev_stage"),
            dev_stage=first_form.get("invest"),
            company=company,
        )
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})

    print('投资公司OK')


def chaxun(company):
    """查询公司数据"""

    # 查询公司标签
    try:
        labels = LabelCompany.objects.filter(company_id=company.id)
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    label_list = []
    for label in labels:
        # print(label.name)
        label_list.append(label.name)
    # 查询创建团队
    try:
        founder = FoundTeam.objects.get(company_id=company.id)
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    # 查询公司产品
    try:
        product = CompanyProduct.objects.get(company_id=company.id)
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    # 查询公司领域
    try:
        lingyu = CompanyDomain.objects.get(company_id=company.id)
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})
    # 查询投资公司信息
    try:
        invest = InvestCompany.objects.get(company_id=company.id)
    except:
        return http.JsonResponse({'errno': constants.DBERROR, 'errmsg': '系统繁忙'})

    # 查询公司职位
    try:
        position = Position.objects.filter(company_id=company.id)
    except:
        position = None

    context = {
        'labels': label_list,
        'founder': founder,
        'product': product,
        'invest': invest,
        'lingyu': lingyu,
        'position': position,
    }

    return context
